package work.vcloud.baseinfo.controller;

import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import work.vcloud.frame.controller.BaseController;
import work.vcloud.frame.dao.OpenDao;
import work.vcloud.frame.vo.OutputVo;

import java.util.Map;

@RestController
@Scope("prototype")
public class ChangeUnitController extends BaseController {

    @Autowired
    OpenDao openDao;

    /**
     * 审核通过单位变更
     *
     * @return
     */
    @RequestMapping("changeUnit/auditPassUnitChange")
    public OutputVo auditPassUnitChange() {
        try {
            Map<String, Object> map = getParameterMap();
            map.remove("page");

            JSONArray values = JSONArray.parseArray(map.get("values").toString());

            for(int i = 0 ; i < values.size() ; i++){

                //更改教师的单位
                map.put("sqlMapId", "changeUnit.updateTeacherUnit");
                map.put("id",values.get(i));
                openDao.update(map);

                String gzdwpkNew = map.get("gzdwpk_new").toString();
                String gzdwpkOld = map.get("gzdwpk_old").toString();
                if(!gzdwpkOld.equals(gzdwpkNew)){
                    //工作单位调整，已成功清除该用户除教师角色以外的所有权限
                    map.put("sqlMapId", "role.deleteUserOtherRole");
                    openDao.update(map);
                }

                //更新变更状态
                map.put("fk_unitchange", map.get("id"));
                map.put("changestate", 1);
                map.put("sqlMapId", "changeUnit.updateUnitChange");
                openDao.update(map);

            }

            return output("success");
        } catch (Exception e) {
            e.printStackTrace();
            return output("9999");
        }
    }

    /**
     * 不通过单位变更
     *
     * @return
     */
    @RequestMapping("changeUnit/auditFailUnitChange")
    public OutputVo auditFailUnitChange() {
        try {
            Map<String, Object> map = getParameterMap();
            map.remove("page");

            //更新变更状态
            map.put("fk_unitchange", map.get("id"));
            map.put("changestate", 2);
            map.put("sqlMapId", "changeUnit.updateUnitChange");
            openDao.update(map);
            return output("success");
        } catch (Exception e) {
            e.printStackTrace();
            return output("9999");
        }
    }

    /**
     * 审核通过单位变更-批量
     *
     * @return
     */
    @RequestMapping("changeUnit/auditPassUnitChangeP")
    public OutputVo auditPassUnitChangeP() {
        try {
            Map<String, Object> map = getParameterMap();
            map.remove("page");






            //更改教师的单位
            map.put("sqlMapId", "changeUnit.updateTeacherUnit");
            openDao.update(map);

            String gzdwpkNew = map.get("gzdwpk_new").toString();
            String gzdwpkOld = map.get("gzdwpk_old").toString();
            if(!gzdwpkOld.equals(gzdwpkNew)){
                //工作单位调整，已成功清除该用户除教师角色以外的所有权限
                map.put("sqlMapId", "role.deleteUserOtherRole");
                openDao.update(map);
            }

            //更新变更状态
            map.put("fk_unitchange", map.get("id"));
            map.put("changestate", 1);
            map.put("sqlMapId", "changeUnit.updateUnitChange");
            openDao.update(map);
            return output("success");
        } catch (Exception e) {
            e.printStackTrace();
            return output("9999");
        }
    }

}
